package com.qingzhu.controller.addressserver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qingzhu.pojo.ReceivingAddress;
import com.qingzhu.pojovo.ResultVo;
import com.qingzhu.service.ReceivingAddressService;
import com.qingzhu.service.impl.ReceivingAddressServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/receiving/address/select")
public class ReceivingAddressSelectController extends HttpServlet {
    ReceivingAddressService receivingAddressService = new ReceivingAddressServiceImpl();
    ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("收货地址查询服务..");

        String userId = req.getParameter("userId");
        List<ReceivingAddress> receivingAddressList = receivingAddressService.findAll(Integer.parseInt(userId));
        ResultVo resultVo = receivingAddressList.size() > 0 ?
                new ResultVo(200,receivingAddressList) : new ResultVo(1000,"您还没有添加收货地址！");
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
}
